package com.ayoka.parenting.ui.group.topic.detail;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CommentBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: TopicDetailModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class TopicDetailModel implements TopicDetailContract.Model{

    @Override
    public void getTopicCommentListData(final BasePresenter presenter, final String topicId, final int pageNo, final int pageSize, final RequestCallback<PagerBean<CommentBean>> callback) {
        API.getTopicCommentListData(presenter, topicId, pageNo, pageSize, new HttpBeanInterceptCallback<BaseResponse<PagerBean<CommentBean>>>() {
            @Override
            public void onSuccess(BaseResponse<PagerBean<CommentBean>> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getTopicCommentListData(presenter, topicId, pageNo, pageSize, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void commentTopic(final BasePresenter presenter, final String replyToUserId, final String replyToTopicID, final String topicId, final String commentContent, final RequestCallback<CommentBean> callback) {
        API.commentTopic(presenter, replyToUserId, replyToTopicID, topicId, commentContent, new HttpBeanInterceptCallback<BaseResponse<CommentBean>>() {
            @Override
            public void onSuccess(BaseResponse<CommentBean> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    commentTopic(presenter, replyToUserId, replyToTopicID, topicId, commentContent, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void praiseComment(final BasePresenter presenter, final String commentId, final RequestCallback<BaseResponse> callback) {
        API.praiseComment(presenter, commentId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    praiseComment(presenter, commentId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void cancelPraiseComment(final BasePresenter presenter, final String commentId, final RequestCallback<BaseResponse> callback) {
        API.cancelPraiseComment(presenter, commentId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    cancelPraiseComment(presenter, commentId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void starComment(final BasePresenter presenter, final String commentId, final RequestCallback<BaseResponse> callback) {
        API.starComment(presenter, commentId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    starComment(presenter, commentId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void cancelStarComment(final BasePresenter presenter, final String commentId, final RequestCallback<BaseResponse> callback) {
        API.cancelStarComment(presenter, commentId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    cancelStarComment(presenter, commentId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
